// components/FriendCircle/index.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      observer: function (newVal, oldVal) {
        //console.log(newVal)
        this.setData({
          lists: newVal
        })
        let nickname = app.globalData.userInfo.nickName;

      }
    },
  },
  lifetimes: {
    attached() {
     try {
      let userinfo = app.globalData.userInfo;
      if (userinfo != undefined) {
        this.setData({
          hasUserInfo: true
        },()=>{
          this.triggerEvent('listener', {
            func:'reqdata'
          });
        })
      }
     } catch (error) {
       console.error
     }
      
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    bottom: 0,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    list: [],
    showZanAndPinglunNum: null,
    isShowOrHideComtent: false,
    allOrPart: "全文",
    inputArea: false,
    commentText: '',
    cid: null,
    replyid: null,
    replynickname: null,
    index: -1
  },
  /**
   * 组件的方法列表
   */
  methods: {
    huifu(e) {
      let cid = e.currentTarget.dataset.idx;
      let rid = e.currentTarget.dataset.rid;
      let rname = e.currentTarget.dataset.rname;
      let index = e.currentTarget.dataset.index;
      this.setData({
        replyid: rid,
        replynickname: rname,
        cid: cid,
        index: index
      }, () => {
        this.setData({
          inputArea: true
        })
      })
    },
    async handleSend() {
      //上传评论数据
      let commentText = this.data.commentText;
      let index = this.data.index;
      console.log(index)
      let commentList = `list[${index}].commentPOList`;
      let tcommentList = this.data.list[index].commentPOList;
      if (this.data.replynickname == null) {
        tcommentList = tcommentList.concat({
          nickname: app.globalData.userInfo.nickName,
          content: commentText,
          replynickname: 'null'
        })
      } else {
        tcommentList = tcommentList.concat({
          nickname: app.globalData.userInfo.nickName,
          content: commentText,
          replynickname: this.data.replynickname
        })
      }
      let Router = app.Router;
      Router.UpCommentInfo(this.data.cid, commentText, this.data.replyid, this.data.replynickname).then(res => {
        console.log(res)
        this.setData({
          inputArea: false,
          replynickname: null,
          replyid: null,
          cid: null,
          [commentList]: tcommentList
        })
      })
    },
    taInput(e) {
      this.setData({
        commentText: e.detail.value.trim()
      })
    },
    pinglun(e) {
      let cid = e.currentTarget.dataset.idx;
      let index = e.currentTarget.dataset.index;
      if (cid != undefined) {
        this.setData({
          showZanAndPinglunNum: null,
          cid: cid,
          index: index
        }, () => {
          this.setData({
            inputArea: true
          })
        })
      }

    },
    //获取焦点
    onbindfocus(e) {
      console.log(e)
      this.setData({
        bottom: e.detail.height,
      })
    },
    tapBtnToGetUserInfo: function (e) {
      if (e.detail.errMsg == "getUserInfo:ok") {
        console.log('获得授权成功')
        app.globalData.userInfo = e.detail.userInfo;
        wx.setStorageSync('wxuserinfo', e.detail.userInfo)
        //console.log(e.detail.userInfo)
        this.setData({
          userInfo: e.detail.userInfo,
          hasUserInfo: true
        },()=>{
           //云开发不做权限隔离
        this.triggerEvent('listener', {
          func:'reqdata'
        });
        })
        /*
          let func = 'pub';
          this.triggerEvent('listener', {func});
          */
      } else {
        console.log('获得授权失败')
      }

    },
    // 点击头部相册图标,打开发朋友圈编辑页面
    showEditPage() {
      let func = 'fpub';
      //这个地方做权限校验
      let Router = app.Router;
      let Config = app.Config;
      var that = this;
      if (Config.SevMode == 'Cloud') {
        //云开发不做权限隔离
        this.triggerEvent('listener', {
          func
        });
      } else {
        this.triggerEvent('listener', {
          func:'auth'
        });
      }

    },
    //朋友圈正文内容显示或收起,逻辑是通过style动态设置max-height样式
    ShowOrHideComtent() {
      if (this.data.allOrPart === "全文") {
        this.setData({
          isShowOrHideComtent: true,
          allOrPart: "收起"
        })
      } else if (this.data.allOrPart === "收起") {
        this.setData({
          isShowOrHideComtent: false,
          allOrPart: "全文"
        })
      }
    },
    //点击朋友圈图片,弹出框预览大图
    showImg(e) {
      let outidx = e.currentTarget.dataset.outidx;
      let imgidx = e.target.dataset.imgidx;
      let imgArr = this.data.list[outidx].images;
      wx.previewImage({
        current: imgArr[imgidx], // 当前显示图片的http链接
        urls: imgArr // 需要预览的图片http链接列表
      })
    },
    //点击评论图标,显示点赞和评论按钮
    showZanAndPinglun(e) {
      // console.log(e.currentTarget.dataset.idx)
      this.setData({
        showZanAndPinglunNum: e.currentTarget.dataset.idx,
        inputArea: false
      })
    },
    //点选和评论的隐藏通过事件委托到全页面(暂时只实现当条朋友所在区域,全页面和滚动时也隐藏在考虑实现)
    hideZanAndPinglun() {
      this.setData({
        showZanAndPinglunNum: null,
        inputArea: false
      })
    },
    //点赞
    ilike(e) {
      let Router = app.Router;
      let cid = e.currentTarget.dataset.idx;
      let index = e.currentTarget.dataset.index;
      //console.log(index)
      wx.showLoading({
        title: '',
        mask:true
      })
      let nickname = app.globalData.userInfo.nickName;
      let thumblist = `list[${index}].thumbPOList`;
      let likelist = `list[${index}].thumblist`;
      let tthumb = this.data.list[index].thumbPOList;
      tthumb = tthumb.concat(nickname)
      Router.UpThumbInfo(cid).then(res => {
        console.log(res);
      
        let tcir = this.data.circleitem;
        this.setData({
          liked: true,
          [thumblist]: tthumb,
          [likelist]: this.data.list[index].thumblist + nickname,
          showZanAndPinglunNum: null
        },()=>{
          wx.hideLoading({
            success: (res) => {},
          })
        })
      })
      //点赞内容 没有重新刷新的必要

      /*
      Router.UpThumbInfo(this.data.cid).then(res=>{
        console.log(res);
        let tcir = this.data.circleitem;
        tcir.thumbsnum = tcir.thumbsnum+1;
        this.setData({
          liked:true,
          circleitem:tcir
        })
      })
      */
    }
  }
})